package com.sqx.modules.order.controller;

import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.order.entity.Evaluate;
import com.sqx.modules.order.entity.TbOrder;
import com.sqx.modules.order.service.AppOrderService;
import com.sqx.modules.order.service.EvaluateService;
import com.sqx.modules.sys.controller.AbstractController;
import com.sqx.modules.sys.entity.SysUserEntity;
import com.sqx.modules.utils.excel.ExcelData;
import com.sqx.modules.utils.excel.ExportExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Api(tags={"管理端-订单"})
@RestController
@RequestMapping("/admin/order")
public class OrderController extends AbstractController {

    @Autowired
    private AppOrderService appOrderService;
    @Autowired
    private EvaluateService evaluateService;

    @ApiOperation("条件查询所有订单")
    @GetMapping(value = "selectAllOrder")
    public Result selectAllOrder(Integer page, Integer limit, Integer status, String phone, Long shopId, String userName, String orderNumber, Integer orderType,String shopName){
        return appOrderService.selectAllOrder(page, limit, status, phone, shopId, userName, orderNumber, orderType,shopName);
    }

    @ApiOperation("条件查询所有订单(管理端)")
    @GetMapping(value = "selectAllOrderAdmin")
    public Result selectAllOrderAdmin(Integer page, Integer limit, Integer status, String phone, Long shopId, String userName,
                                      String orderNumber, Integer orderType,String shopName,String riderPhone, String startTime, String endTime){
        return appOrderService.selectAllOrderAdmin(page, limit, status, phone, shopId, userName, orderNumber, orderType,shopName,riderPhone,startTime,endTime);
    }

    @GetMapping("excelOrder")
    @ApiOperation("导出卡密列表")
    public void excelOrder(Integer status, String phone, Long shopId, String userName,
                           String orderNumber, Integer orderType,String shopName,String riderPhone,
                           String startTime, String endTime, HttpServletResponse response) throws Exception{
        ExcelData excelData = appOrderService.excelAllOrderAdmin(status, phone, shopId, userName, orderNumber, orderType, shopName, riderPhone, startTime, endTime);
        ExportExcelUtils.exportExcel(response,"订单列表.xlsx",excelData);
    }


    @ApiOperation("完成订单")
    @PostMapping(value = "accomplishOrder")
    public Result accomplishOrder(Long orderId){
        TbOrder tbOrder2 = appOrderService.getById(orderId);
        if(tbOrder2.getAutoSendOrder()!=null && tbOrder2.getAutoSendOrder()==1){
            return appOrderService.accomplishOrder( orderId);
        }
        return Result.error("当前订单为自动派单，不能手动完成！");
    }

    @ApiOperation("管理订单")
    @PostMapping(value = "updateOrder")
    public Result updateOrder(@RequestBody TbOrder tborder){

        return appOrderService.updateOrder(tborder);
    }

    @ApiOperation("管理端退款")
    @PostMapping(value = "adminCancelOrder")
    public Result adminCancelOrder(Long orderId){

        return appOrderService.adminCancelOrder(orderId);
    }

    @ApiOperation("商家回复评论")
    @PostMapping(value = "shopReplyEvaluate")
    public Result shopReplyEvaluate(String orderNumber, String shopReplyMessage){

        return appOrderService.shopReplyEvaluate(orderNumber, shopReplyMessage);
    }

    @ApiOperation("商家删除评论")
    @PostMapping("/deleteEvaluate")
    public Result deleteEvaluate(Long evaluateId){
        evaluateService.removeById(evaluateId);
        return Result.success();
    }

    @ApiOperation("打印票")
    @PostMapping(value = "/print")
    public Result print(Long orderId){
        return appOrderService.print(orderId);
    }

    @ApiOperation("获取用户评价列表")
    @GetMapping(value = "getEvaluateList")
    public Result getEvaluateList( Long userId, Evaluate evaluate, Integer page, Integer limit){
        evaluate.setUserId(userId);
        return Result.success().put("data", appOrderService.getEvaluateList(evaluate,page,limit));
    }

}
